#include <stdio.h>
#include <stdlib.h>

/*  Fibonacci.cpp
*   This program is part of the °Simple-Benchs° Benchmark
*   The complexity of this program is O(n) where n is the position of the FIbonacci number in the sequence.
*
*   
*   To run the program: ./program number_position
*/

/* Perform the computation to get the N element of the fibonacci sequence. 
For n > 2 the complexity is O(n) defined by the loop in the line 18*/
void fib_calc(int n) {
  unsigned long int first = 1;
  unsigned long int second = 1;
  unsigned long int  third;
  if (n <= 2){
    printf("Fib(%d) = 1\n\n",n);  
  }
  else{
    for (int i = 0; i < n - 2; ++i) {
      third = first + second;
      second = first;
      first = third;
    }
    printf("Fib(%d) = %lu\n\n",n, third);
  }
}

/*  Initial check to verify whether the amount of arguments is correct or not.  Complexity O(1) */
short initial_check(int inputs){
  if (inputs < 2) { 
    fprintf(stderr,"Wrong number of argments. Syntax: ./program number_position\n");
    return 1;
  }
  return 0;
}


short second_check(int value){
  if (value < 0){
    fprintf(stderr,"Negative number [%d]. Please enter a positive value\n", value);
    return 1;
  }else if(value > 90){
    fprintf(stdout,"Warning. %d is a big number. The value below may be wrong.\n", value);
  }

  return 0;
}

int main(int argc, char** argv){

  /* Checking arguments */
  if (initial_check(argc)) return 1;

  /* Get parameters */
  int element = atoi(argv[1]);

  /* Checking arguments */
  if (second_check(element)) return 1;  

  /* Calculating the fibonacci sequence */
  fib_calc(element);

  return 0;
}